package com.book.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.common.entity.QueryRequest;
import com.book.warehouse.entity.WarehouseBook;
import com.book.warehouse.mapper.WarehouseBookMapper;
import com.book.warehouse.service.WarehouseBookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 刘恬镯
 * @Description:  20174052
 */

@Service
public class WarehouseBookServiceImpl extends ServiceImpl<WarehouseBookMapper, WarehouseBook> implements WarehouseBookService {
    @Autowired
    WarehouseBookMapper warehouseBookMapper;

    @Override
    public List<WarehouseBook> findWarehouseBook(WarehouseBook warehouseBook) {
        QueryWrapper<WarehouseBook> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(warehouseBook.getPutaway())) {
            queryWrapper.eq("putaway", warehouseBook.getPutaway());
        }
        if (StringUtils.isNotEmpty(warehouseBook.getIntoware())) {
            queryWrapper.eq("intoware", warehouseBook.getIntoware());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<WarehouseBook> pageWarehouseBook(QueryRequest request, WarehouseBook warehouseBook) {
        QueryWrapper<WarehouseBook> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(warehouseBook.getBookname())) {
            queryWrapper.like("bookname", warehouseBook.getBookname());
        }
        if (StringUtils.isNotEmpty(warehouseBook.getType())) {
            queryWrapper.like("type", warehouseBook.getType());
        }
        if (StringUtils.isNotEmpty(warehouseBook.getFlag())) {
            queryWrapper.eq("flag", warehouseBook.getFlag());
        }
        if (StringUtils.isNotEmpty(warehouseBook.getIntoware())) {
            queryWrapper.eq("intoware", warehouseBook.getIntoware());
        }
        Page<WarehouseBook> warehouseBookIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(warehouseBookIPage, queryWrapper);
    }

    @Override
    public IPage<WarehouseBook> WarehouseBookApplyPage(QueryRequest request, WarehouseBook warehouseBook, String status) {
        QueryWrapper<WarehouseBook> queryWrapper = new QueryWrapper<>();
        if ("0".equals(status)) {
            queryWrapper.inSql("bookid", "SELECT bookid FROM shelves_apply WHERE status = 0");
        }
        if (StringUtils.isNotEmpty(warehouseBook.getBookname())) {
            queryWrapper.like("bookname", warehouseBook.getBookname());
        }
        if (StringUtils.isNotEmpty(warehouseBook.getType())) {
            queryWrapper.like("type", warehouseBook.getType());
        }
        Page<WarehouseBook> warehouseBookIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(warehouseBookIPage, queryWrapper);
    }

    @Override
    public void addWarehouseBook(WarehouseBook warehouseBook) {
        this.save(warehouseBook);
    }

    @Override
    public void updateWarehouseBook(WarehouseBook warehouseBook) {
        this.saveOrUpdate(warehouseBook);
    }

    @Override
    public void deleteWarehouseBook(String[] warehouseBook) {
        List<String> list = Arrays.asList(warehouseBook);
        this.removeByIds(list);
    }

    @Override
    public List<WarehouseBook> askWarehouseBook(String[] warehouseBook) {
        List<String> list = Arrays.asList(warehouseBook);
        return (List<WarehouseBook>) this.listByIds(list);
    }
}
